import { createRouter, createWebHistory } from 'vue-router';
import LoginView from '../views/login/LoginView.vue'//导入登陆组件
import FirstView from '../views/first/FirstView.vue'//导入首页组件
import TicketView from '../views/ticket/ticketView.vue';//导入神券组件
import OrderListView from '../views/order/OrderListView.vue'//导入订单主页面组件
//导入订单的子组件
import AllOrder from '../views/order/orderList/AllOrder.vue'//订单页面子组件1
import EvaluatingList from '../views/order/orderList/EvaluatingList.vue'//订单页面子组件2
import RefundList from '../views/order/orderList/RefundList.vue'//订单页面子组件3
import BoughtStore from '../views/order/orderList/BoughtStore.vue'//订单页面子组件4
//订单子组件导入完毕
import MyHome from '../views/person/MyView.vue'//导入个人组件
import SearchView from '../views/first/SearchView.vue'//导入搜索组件
import BusinessView from '../views/business/BusinessView.vue'//导入商家内容的组件
//导入商家内容的子组件
import OrderDishesView from '../views/business/items/OrderDishesView.vue';//商家内容子组件1
import AddAddress from '../views/business/items/AddAddress.vue';//商家内容子组件1
import SuperDealsView from '../views/business/items/SuperDealsView.vue';//商家内容子组件2
import AppraiseView from '../views/business/items/AppraiseView.vue';//商家内容子组件3
import BusinessInfoView from '../views/business/items/BusinessInfoView.vue';//商家内容子组件4

//商家内容的子组件导入完毕

import RegisterView from '../views/register/registerView.vue';//导入登录的组件
import ClassBusinessView from '../views/class-business/ClassBusinessView.vue';//导入商家分类展示的页面
import PayView from '../views/pay/PayView.vue';//导入支付的页面

import PersonInfo from '../views/person/PersonInfo.vue'
import UpdateName from '../views/person/UpdateInfo/UpdateName.vue'
import UpdateUsername from '../views/person/UpdateInfo/UpdateUsername.vue'
import UpdateIntro from '../views/person/UpdateInfo/UpdateIntro.vue'
import UpdateAvatar from '../views/person/UpdateInfo/UpdateAvatar.vue'
import SettingView from '../views/person/SettingView.vue'
import UpdateAddress from '../views/person/UpdateAddress.vue'
import AddressList from '../views/person/AddressList.vue'
import AddAddress1 from '../views/person/AddAddress.vue'
// 定义路由配置
const routes = [

    // 登陆页面
    {
        path: '/login',
        name: 'login',
        component: LoginView,
    },
    //注册页面
    {
        path: '/register',
        name: 'register',
        component: RegisterView
    },
    // 首页
    {
        path: '/first',
        name: 'first',
        component: FirstView,

    },
    // 神券
    {
        path: '/ticket',
        name: 'ticket',
        component: TicketView,
    },
    // 订单

    {
        path: '/orderList',
        component: OrderListView,
        //订单页面的子组件
        children: [
            {
                path: 'allOrder',
                name: 'allOrder',
                component: AllOrder,
            },
            {
                path: 'evaluatingList',
                name: 'evaluatingList',
                component: EvaluatingList,
            },
            {
                path: 'refundList',
                name: 'refundList',
                component: RefundList,
            },
            {
                path: 'boughtStore',
                name: 'boughtStore',
                component: BoughtStore
            },
        ],
        redirect: { name: 'allOrder' }
    },
    // 个人
    {
        path: '/myHome',
        component: MyHome
    },
    //个人信息页
    {
        path: '/personInfo',
        component: PersonInfo
    },
    //修改昵称页
    {
        path: '/updateName',
        component: UpdateName
    },
    //修改账户页
    {
        path: '/updateUsername',
        component: UpdateUsername
    },
    //修改简介页
    {
        path: '/updateIntro',
        component: UpdateIntro
    },
    {
        path: '/updateAvatar',
        component: UpdateAvatar
    },
    //设置页
    {
        path: '/setting',
        component: SettingView
    },
    //地址管理页
    {
        path: '/addressList',
        component: AddressList
    },
    //修改地址页
    {
        path: '/updateAddress',
        component: UpdateAddress
    },
    //新增地址页
    {
        path: '/addAddress1',
        component: AddAddress1
    },
    // 根路由:首页:重定向回首页
    {
        path: '/',
        redirect: '/first', // 重定向到 '/first' 路由
    },
    // 搜索
    {
        path: '/search',
        name: 'search',
        component: SearchView,
    },
    //商家的内容
    // {
    //     path: '/business',
    //     name: 'business',
    //     component: BusinessView,

    // },
    {
        path: '/business',
        name: 'business',
        component: BusinessView,
        //商家页面的子组件
        children: [

            {
                path: 'super-deals',
                name: 'SuperDeals',
                component: SuperDealsView,
            },
            {
                path: 'order-dishes',
                name: 'OrderDishes',
                component: OrderDishesView,
            },
            {
                path: 'appraise',
                name: 'Appraise',
                component: AppraiseView,
            },
            {
                path: 'business-info',
                name: 'BusinessInfo',
                component: BusinessInfoView,
            },
        ],
        // redirect: { name: 'OrderDishes' }
    },

    //分类对应的商家
    {
        path: '/class-business',
        name: 'classBusiness',
        component: ClassBusinessView
    },

    //添加收货地址
    {
        path: '/addAddress',
        name: 'AddAddress',
        component: AddAddress
    },
    //收货界面
    {
        path: '/pay',
        name: 'Pay',
        component: PayView
    }
    //...其他路由
];

// 创建 router 实例
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes,
});
// 添加全局前置守卫
router.beforeEach((to, from, next) => {
    const jwt = localStorage.getItem('token');

    if (to.path.startsWith('/orderList') || to.path === '/business' || to.path === '/ticket' || to.path === '/myHome') {
        if (!jwt) {
            // 将目标路由路径作为参数传递给登录页面
            next({
                path: '/login',
                query: { redirect: to.path }
            });
        } else {
            next();
        }
    } else {
        next();
    }
});
export default router;